angular.module('Controller')

.controller('QgyActiveManageCtrl', ($scope,menuManage,NgTableParams,$localStorage,wfAlert,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    $scope.costTypeList=commonConstant.enterFeeTypeList(true)
    $scope.activeStatusList=commonConstant.activeStatusList(true)
    initSearchData = ()->
        $scope.search = {
            type: undefined ,
            status:undefined
            filter:undefined
        }
    $scope.activeTypeList=[{code:undefined,name:'全部'}]
    loadActiveType=()->
        $http.get('/dictionary?dicIndex=3&page=0&size=999')
        .success((resp)->
            if resp.code==0
                $scope.activeTypeList=$scope.activeTypeList.concat(resp.message.content)
                $scope.tableParams.reload()
        )
    loadActiveType()
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        requestData={
            page: params.page() - 1,
            size: params.count(),
            status:$scope.search.status,
            filter: $scope.search.filter,
            type:$scope.search.type
        }
        getParam=commonUtil.transferGetParam(requestData)
        $http.get('/activity/backend'+getParam)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        viewData:(data)->
            $rootScope.pageType='edit'
            $rootScope.pageParams=data
            menuManage.goState('active-detail',true)
#            theData=commonUtil.cloneObj(data)
#            showModal('edit',theData)
        verifyData:(data)->
            modal=commonUtil.showVerifyModal('活动审核',data.id,data.name)
            modal.result.then((modalData)->
                status=if modalData.result==1 then 1 else 4
                $http.put('/activity/'+data.id,{status:status,reward:modalData.reason})
                .success((resp)->
                    if resp.code==0
                        wfAlert.toastInfo('审核完成')
                        $scope.queryData()
                    else
                        wfAlert.toastError('审核失败')
                )
            )
        showActiveType:(type)->
            tmp=_.find($scope.activeTypeList,(item)->item.code==type)
#            console.log($scope.activeTypeList)
            if tmp
                return tmp.name
            else
                return type

#        removeData:(item)->
#            $http.delete('/activity/'+item.id)
#            .success((resp)->
#                if resp.code==0
#                    wfAlert.toastInfo('活动删除成功')
#                    $scope.queryData()
#                else
#                    wfAlert.toastError(resp.message)
#            )
        startActive:(item)->
            return if item.status!=1 #待进行
            $http.put('/activity/'+item.id,{status:2})
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('活动启动成功')
                    $scope.queryData()
                else
                    wfAlert.toastError(resp.message)
            )
        endActive:(item)->
            return if item.status!=2 #进行中
            $http.put('/activity/'+item.id,{status:3})
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('活动结束成功')
                    $scope.queryData()
                else
                    wfAlert.toastError(resp.message)
            )
        checkData:(item)->
                ''
            resetSearch: ->
                initSearchData()
            queryData: (keepPage)->
                if(keepPage != true)
                    $scope.tableParams.page(1)
                $scope.tableParams.reload()
)